import { Graphic } from "/graphic/type";
import { NodeType } from "./../enum/NodeTypeEnum";
import { Painter } from "./painter";
import { Grender, IGrenderOps } from "/grender";
import Handler from "/handler";
import Node from "./node";
import { Shape } from "/graphic";
interface AddLayerAttribute {
	painter?: Painter;
	handler?: Handler;
}
let id=1;
export class Layer {
	_id=id++;
	painter: Painter;
	handler: Handler;
	type: NodeType.LAYER=NodeType.LAYER;
	children:Graphic[]=[]
	parent:Grender
	constructor() {
		
	}
	add(shape: Graphic | Graphic[]) {
		if(Array.isArray(shape)){
			for(let i=0;i<shape.length;i++){
				this.add(shape[i])
			}
			return;
		}
		if (shape.bindRender) {
			throw new Error("改元素已经成功绑定了render上下文");
		}
		shape.bingContext(this);
		this.children.push(shape);
		shape.setParent(this)
		if(this.parent){
			this.refresh();
		}
	}
    refresh(){
        this.painter.clear();
        this.painter.refresh();
    }
	addLayerToAttributes({ painter, handler }: AddLayerAttribute) {
		if(painter){
			this.painter = painter;
		}
		if(handler){
			this.handler = handler;	
		}
	}
	destory() {
		this.painter.destory();
		this.handler = null;
	}
}
